<?php
header("Content-type:text/html;charset=utf-8");
require_once("dbtools.inc.php");
$id=$_COOKIE['id'];
$correct_t=$_COOKIE['correct'];

$link = create_connection();
$sql="select * from users where id=$id and status='normal'";
$result=@execute_sql($link,'zhihu',$sql);

$user = @mysqli_fetch_object($result)->username;
$user = $user.'thonsun123';
if($correct_t!=md5($user)){
    @mysqli_free_result($result);
    @mysqli_close($link);
    header("location:login.php");
}


$title=$_POST['header'];
$abstract=$_POST['abstract'];
$content=$_POST['content'];
$qtype=$_POST['qtype'];
$addtime = date("Y-m-d H:i:s",time());
$img = rand(1,30);

$sql = "insert into questions(qtypeid,users_id,abstract,title,content,addtime,img) value ($qtype,$id,'$abstract','$title','$content','$addtime',$img)";
$result=execute_sql($link,'zhihu',$sql);

$sql = "select * from questions order by id desc limit 0,1";
$result = execute_sql($link,'zhihu',$sql);
$question_id=@mysqli_fetch_object($result)->id;

$sql = "update users set questions=questions+1 where id={$id}";
$result=execute_sql($link,'zhihu',$sql);

foreach ($_POST['invited_id'] as $i){
        $sql = "insert into invite (users_id,invite_id,questions_id,addtime) value ($id,$i,$question_id,'$addtime')";
        echo $sql;
        $result = execute_sql($link,'zhihu',$sql);
    }

@mysqli_free_result($result);
@mysqli_close($link);
echo "<script>alert('添加成功');history.back();</script>";
?>